package com.example.ibeacon_station;


import static androidx.core.app.ActivityCompat.startActivityForResult;

import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.le.BluetoothLeScanner;
import android.bluetooth.le.ScanCallback;
import android.bluetooth.le.ScanResult;
import android.bluetooth.le.ScanSettings;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;

public class BluetoothProcess {
    private static final int REQUEST_ENABLE_BT = 871;
    private Context mContext;
    private static BluetoothAdapter bluetoothAdapter;
    private static AlertDialog bluetoothWarningDialog;

    public BluetoothProcess(Context context) {
        mContext = context;
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        checkBluetoothLeSupport(); //检查设备是否支持蓝牙
        if(bluetoothAdapter.isEnabled()){
            startScan();
        } else  {

        }

    }

    // Check hardware support
    private void checkBluetoothLeSupport() {
        if (bluetoothAdapter == null || !mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH_LE)) {
            SystemService.fatalWarning(mContext, "硬件不支持", "对不起，本设备不支持蓝牙4.0 BLE，无法正常使用本软件");
        }
    }

    // Open bluetooth
    public static void openBluetooth() {
        if (!bluetoothAdapter.isEnabled()) {
            bluetoothAdapter.enable();
        }
    }

    public static void restartBluetooth(){
        if (!bluetoothAdapter.isEnabled()) {
            bluetoothAdapter.enable();
        }
        else {
            bluetoothAdapter.disable();
            bluetoothAdapter.enable();
        }
    }

    private void startScan(){
        BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner();
        ScanSettings ss = new ScanSettings.Builder().setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY).build();
        bluetoothLeScanner.startScan(null, ss, mScanCallback);
        // When new BLE detected, auto invoke onScanResult().
    }

    // 回调
    private ScanCallback mScanCallback = new ScanCallback() {
        @Override
        public void onScanResult(int callbackType, ScanResult result) {
            ((MainActivity)mContext).beaconCallback(result);
        }
    };

    /**
     * 处理蓝牙关闭的对话框，重新打开蓝牙
     */
    public void bluetoothWarning(boolean show) {
        if (bluetoothWarningDialog == null) {
            bluetoothWarningDialog = new AlertDialog.Builder(mContext)
//                    .setIcon(R.drawable.ic_bluetooth_disabled_black_24dp)
                    .setTitle("蓝牙已关闭")
                    .setMessage("无法继续扫描Beacon与定位，打开蓝牙吗？")
                    .setPositiveButton("好的", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            openBluetooth();
                        }
                    })
                    .setNegativeButton("退出应用", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            System.exit(0);
                        }
                    })
                    .setCancelable(false).create();
        }
        if(show && !bluetoothWarningDialog.isShowing()){
            bluetoothWarningDialog.show();
        }
        else if(!show && bluetoothWarningDialog.isShowing()) {
            bluetoothWarningDialog.dismiss();
        }
    }

}
